import { Component, OnInit } from '@angular/core';
import { DeviceService } from '../../../share/device.service'

@Component({
  selector: 'app-left-nav',
  templateUrl: './left-nav.component.html',
  styleUrls: ['./left-nav.component.css']
})
export class LeftNavComponent implements OnInit {
  public status: string;
  public trees: Array<Trees>;
  constructor(public device: DeviceService) { }

  ngOnInit() {
    this.trees = [
      {
        parent: {
          orgID: 'org001',
          name: '变压器出线50'
        } ,
        children: [
          {
            meterID: '123456123456',
            name: '电容器'
          },
          {
            meterID: '001234561234',
            name: '原料气压缩机'
          }
        ]
      },
      {
        parent: {
          orgID: 'org002',
          name: '变压器出线50#2'
        } ,
        children: [
          {
            meterID: '3',
            name: '电容器#2'
          },
          {
            meterID: '4',
            name: '原料气压缩机#2'
          }
        ]
      }
    ];
    if ( sessionStorage.getItem( 'device' ) === null) {
      sessionStorage.setItem('device', JSON.stringify(this.trees[0]['children'][0]) );
      this.sendChild(this.trees[0]['children'][0])
    } else {
      this.status = JSON.parse(sessionStorage.getItem( 'device' )).meterID;
      this.sendChild( JSON.parse(sessionStorage.getItem( 'device' ) ));
    }
  }

  sendChild(device) {
    sessionStorage.setItem('device', JSON.stringify(device) );
    this.status = device.meterID;
    this.device.sendMessage(device);
  }
}

export class Child {
  meterID: string;
  name: string;
}
export class Parent {
  orgID: string;
  name: string;
}

export class Trees {
  parent: Parent;
  children: Array<Child>;
}
